package com.cjd.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.cjd.common.R;


/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2017-07-04
 * description 垂直字母排序view A-Z
 */

public class LetterSideBar extends View {

    //触摸事件
    private OnTouchingLetterChangedListener onTouchingLetterChangedListener;

    // 26个字母
    public static String[] b = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z", "#"};
    //选中
    private int choose = -1;

    private Paint paint;

    protected TextView mTextDialog;

    private int fontSize;
    private int fontColor;
    private int fontActiveColor;
    private int bgColor;
    private int bgActiveColor;

    /**
     * 为SideBar显示字母的TextView
     *
     * @param mTextDialog
     */
    public void setTextView(TextView mTextDialog) {
        this.mTextDialog = mTextDialog;
    }


    public LetterSideBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    public LetterSideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public LetterSideBar(Context context) {
        super(context);
        init(context, null, 0);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        fontSize = dip2px(14);
        paint = new Paint();
        fontColor = Color.parseColor("#009ff0");
        fontActiveColor = Color.parseColor("#ffffff");
        bgColor = Color.TRANSPARENT;
        bgActiveColor = Color.parseColor("#55000000");

        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LetterSideBar);
            fontSize = ta.getDimensionPixelSize(R.styleable.LetterSideBar_lsb_font_size, fontSize);
            fontColor = ta.getColor(R.styleable.LetterSideBar_lsb_font_color, fontColor);
            fontActiveColor = ta.getColor(R.styleable.LetterSideBar_lsb_font_active_color, fontActiveColor);
            bgColor = ta.getColor(R.styleable.LetterSideBar_lsb_bg_color, bgColor);
            bgActiveColor = ta.getColor(R.styleable.LetterSideBar_lsb_bg_active_color, bgActiveColor);
            ta.recycle();
        }

    }

    /**
     * 重写的onDraw的方法
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getHeight();//获取对应的高度
        int width = getWidth();//获取对应的宽度
        int singleHeight = height / b.length;//获取每一个字母的高度
        for (int i = 0; i < b.length; i++) {
            paint.setColor(fontColor);//默认颜色 009ff0
            paint.setTypeface(Typeface.DEFAULT_BOLD);
            paint.setAntiAlias(true);
            paint.setTextSize(fontSize);
            //选中的状态
            if (i == choose) {
                paint.setColor(fontActiveColor);
                paint.setFakeBoldText(true);//设置是否为粗体文字
            }
            //x坐标等于=中间-字符串宽度的一般
            float xPos = width / 2f - paint.measureText(b[i]) / 2;
            float yPos = singleHeight * i + singleHeight;
            canvas.drawText(b[i], xPos, yPos, paint);
            paint.reset();//重置画笔
        }
        setBackgroundColor(bgColor);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        final int action = event.getAction();
        final float y = event.getY();//点击y坐标
        final int oldChoose = choose;

        final int c = (int) (y / getHeight() * b.length);//点击y坐标所占高度的比例*b数组的长度就等于点击b中的个数

        switch (action) {
            case MotionEvent.ACTION_UP:
                setBackgroundColor(bgColor);
//                setBackgroundDrawable(new ColorDrawable(0x00000000));//设置背景颜色
                choose = -1;
                invalidate();
                if (mTextDialog != null) {
                    mTextDialog.setVisibility(View.INVISIBLE);
                }
                break;

            default:
                setBackgroundColor(bgActiveColor);
                if (oldChoose != c) {
                    if (c >= 0 && c < b.length) {
                        if (onTouchingLetterChangedListener != null) {
                            onTouchingLetterChangedListener.onTouchingLetterChanged(b[c]);
                        }
                        if (mTextDialog != null) {
                            mTextDialog.setText(b[c]);
                            mTextDialog.setVisibility(View.VISIBLE);
                        }
                        choose = c;
                        invalidate();
                    }
                }
                break;
        }


        return true;
    }

    /**
     * 向外松开的方法
     *
     * @param onTouchingLetterChangedListener
     */
    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    /**
     * 接口
     *
     * @author
     */
    public interface OnTouchingLetterChangedListener {
        public void onTouchingLetterChanged(String s);
    }

    private int dip2px(float var1) {
        float var2 = getResources().getDisplayMetrics().density;
        return (int) (var1 * var2 + 0.5F);
    }
}
